<?php
// @formatter:off
/**
 * @file c.tags.php
 *
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.tags.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/themes/mdefault/controlers/contents/c.tags.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcDefaults;
	global $wcPageCache;
	global $wcTagsHolder;
	global $wctDefaults;

	$wcPageCache->anyTag();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minTagInPage = $wctDefaults["max-per-page"]["tags"] * ($page - 1);
	$maxTagInPage = $minTagInPage + $wctDefaults["max-per-page"]["tags"];

	$wcThemeAssigns["PATH"] = "All Tags (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/generics/list-view.html";

	//
	// Selecting only tags in range.
	$tags = array();
	$tagCount = 0;
	foreach($wcTagsHolder->itemIds($wcGroup) as $id) {
		if($tagCount >= $minTagInPage && $tagCount < $maxTagInPage) {
			$tag = $wcTagsHolder->item($id, $wcGroup);
			if($tag->ok() && $tag->count) {
				$tags[] = $tag;
			} else {
				$tagCount--;
			}
		}
		$tagCount++;
	}

	$pages = ceil($tagCount / $wctDefaults["max-per-page"]["tags"]);
	wcSetPager(requestParams(true, $wcAction, array('page')), $pages, $page);
	if($pages > 1) {
		$wcThemeAssigns["PAGER-SHOW"] = true;
		$wcThemeAssigns["PAGER-CURRENT"] = $page;
		$wcThemeAssigns["PAGER-COUNT"] = $pages;
	}

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	$gid = $wcGroup->id();
	$iconId = 1;
	$zoom = array();
	foreach($tags as $tag) {
		$name = $tag->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = array(
			"name" => $tag->name,
			"title" => $tag->name,
			"iconid" => $iconId,
			"icon" => $tag->preview(),
			"url" => "?action=".WC_ACTION_TAG_SHOW."&group={$gid}&tag={$tag->id}"
		);

		$zoom[] = array(
			"id" => $iconId,
			"path" => $tag->preview()
		);

		$iconId++;
	}
	ksort($lists);

	if(count($lists) == 0) {
		$lists[] = array(
			"prefix" => "No available tags",
			"list" => array()
		);
	}

	$wcThemeAssigns["MenuLists"] = $lists;
	$wcThemeAssigns["MenuListsZoom"] = $zoom;
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list tags";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>